import Axios from "axios";
import { baseUrl } from "./config";
import qs from "qs";
import { NotifyPlugin } from "tdesign-vue";
const request = Axios.create({
  timeout: 60000,
});

// 请求拦截器
request.interceptors.request.use(
  (config) => {
    config.baseURL = config.baseUrl || baseUrl;
    if (config.method === "get") {
      // 如果是get请求，且params是数组类型如arr=[1,2]，则转换成arr=1&arr=2
      config.paramsSerializer = function (params) {
        return qs.stringify(params, { arrayFormat: "repeat" });
      };
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
request.interceptors.response.use(
  (response) => {
    let err = response.data?.Error;
    if (err) {
      NotifyPlugin("error", {
        title: "错误",
        content: err,
      });
      return Promise.reject(err);
    }
    return response.data;
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default request;
